package DDSDownloadServer;

import DDSInterfaces.ServerBandwidthBean;
import java.rmi.*;
import java.rmi.registry.*;
import java.util.ArrayList;

import jade.lang.acl.*;
import jade.content.*;
import jade.content.onto.basic.*;
import jade.content.lang.sl.*;
import jade.core.*;
import jade.domain.mobility.*;
import jade.domain.JADEAgentManagement.*;
import java.util.ArrayList;
import jade.core.Agent;
import jade.wrapper.StaleProxyException;

/**
 *
 * @author Rasmus Bo Sorensen
 * @author Bo Visfeldt
 */
public class DownloadServer extends java.rmi.server.UnicastRemoteObject implements DDSInterfaces.IDownloadServer {
    // The ServerInfoList is loaded in the Download server and passed on to the created Agents.
    private ArrayList<ServerInfo> serverInfoList;
    private ServerInfoLoader serverInfoLoader = new ServerInfoLoader();
    Registry registry;
    private jade.wrapper.AgentContainer agentMainContainer;
    private int createdAgents = 0;

    public static void main(String[] args) throws Exception {
        new DownloadServer(args[0]);
    }

    public DownloadServer(String path) throws Exception{
        // Loading List of File server to visit.
        serverInfoList = serverInfoLoader.LoadInfo(path);
        setupJadeMainContainer();       // Settingup Main Jade Container.
        setupRmi();                 // Settingup RMI service for client.
    }

    public ServerBandwidthBean RequestServer(String aTitle, String clientContainerName) throws RemoteException{
        try{
            createNewAgent(aTitle,clientContainerName); //creating a new agent
        }
        catch(Exception e){
            e.printStackTrace();
        }
        
        ServerBandwidthBean serverBandwidthBean = new ServerBandwidthBean(null, 0);
        return serverBandwidthBean;
    }
    
    private void setupJadeMainContainer() throws Exception{
        //create new JadeMainContainer;
        try{
            System.out.println("Fetching JADE runtime enviroment...");
            jade.core.Runtime runtime = jade.core.Runtime.instance();
            System.out.println("Creating JADE main container...");
            ProfileImpl profileToAvoidCrappyLinuxFailure = new ProfileImpl("127.0.0.1",1099, "MainAmsId");
            agentMainContainer = runtime.createMainContainer(profileToAvoidCrappyLinuxFailure);

        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }        
    }

    private int createNewAgent(String aTitle, String clientContainerName) throws StaleProxyException{
        System.out.println("Entering createNewAgent()");
        createdAgents++;
        Object[] args = new Object[3];
        args[0] = aTitle;
        args[1] = clientContainerName;
        args[2] = new ArrayList<ServerInfo>(serverInfoList);
        System.out.println("Passing args.");
        jade.wrapper.AgentController agentController = agentMainContainer.createNewAgent("QueryAgent-"+createdAgents,"DDSDownloadServer.QueryAgent",args);
        System.out.println("QueryAgent created...");
        agentController.start();
        return createdAgents;
    }

    private void setupRmi() throws RemoteException{
        //there is only one downloadserver, therefore we are always bindind it to localhost
        //in reality this would be a server on the net, but this is just for testing.

        int port = 2221;
        String host = "localhost";
        String name = "DownloadServer";

        System.out.println("Starting RMI Download Server: 'DownloadServer+' @ "+host+":"+port+"...");
       //creating server and binding to registry
       try{
            registry = LocateRegistry.createRegistry(port);
            registry.rebind(name, this);
        }
        catch(RemoteException e){
            throw e;
        }
        System.out.println("Server RMI Download Server Started");
    }
}
